﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Linq;

namespace ByrForum
{
    
    public partial class BoardList : PhoneApplicationPage
    {
        bool if_loaded = false;
        List<BoardInfo> listBoard = new List<BoardInfo>();
        public BoardList()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            IDictionary<string, string> queryString = this.NavigationContext.QueryString;
            string index = "",title="";
            try
            {
                queryString.TryGetValue("title", out index);
            }
            catch
            {
                base.OnNavigatedTo(e);
                return;
            }
            switch (index)
            { 
                case "0":
                    title = "北邮校园";
                    break;
                case "1":
                    title = "学术科技";
                    break;
                case "2":
                    title = "信息社会";
                    break;
                case "3":
                    title = "人文艺术";
                    break;
                case "4":
                    title = "休闲娱乐";
                    break;
                case "5":
                    title = "生活时尚";
                    break;
                case "6":
                    title = "体育健身";
                    break;
                case "7":
                    title = "游戏对战";
                    break;
                default:
                    break;

            }
            if (!if_loaded)
            {
                this.PageTitle.Text = title;
                
                String myRequestUrl = ByrApi.apiUrl + "/section/"+(Convert.ToInt16(index)+1).ToString() + ByrApi.Encode + ByrApi.appkey;
                HttpUtil.HttpGet(myRequestUrl, HttpWebRequset_Callback);
                Debug.WriteLine(myRequestUrl);
  
                 progressBarWait.Visibility = Visibility.Visible;

                if_loaded = true;
            
            }

            base.OnNavigatedTo(e);
        }
        private void HttpWebRequset_Callback(IAsyncResult asynchronousResult)
        {
            this.Dispatcher.BeginInvoke(
                () => {
                    listboxBoard.Items.Clear();
                    listBoard.Clear();
                }
                );
            try
            {
                string result = HttpUtil.ReadToEnd(asynchronousResult);

                DebugInfo.writeXml(result);
                string xmlstr = SanitizeXmlString(result);
                try
                {
                    XDocument doc = XDocument.Parse(xmlstr);
                    foreach (XElement item in doc.Descendants("board").Nodes())
                    {
                        if (item.Name == "item")
                        {
                            string name = "", description = "";
                            foreach (XElement child_item in item.Elements())
                            {
                                switch (child_item.Name.ToString())
                                { 
                                    case "name":
                                        name = child_item.Value;
                                      //  Debug.WriteLine("Name:"+name);
                                        break;
                                    case "description":
                                        description = child_item.Value;
                                        this.Dispatcher.BeginInvoke(
                                            () => {
                                                listboxBoard.Items.Add(description);
                                            }
                                            );
                                        break;
                                    default:
                                        break;

                                }
                                
                            }
                            listBoard.Add(new BoardInfo(name, description));
                        }
                    }
                    this.Dispatcher.BeginInvoke(
                            () => {
                                this.progressBarWait.Visibility = Visibility.Collapsed;
                            }
                        );
                }
                catch (Exception e){
                    Debug.WriteLine(e.Message);
                    this.Dispatcher.BeginInvoke(
                            () =>
                            {
                                this.progressBarWait.Visibility = Visibility.Collapsed;
                            }
                        );
                }



            }
            catch { 
            
            }
        }
        public string SanitizeXmlString(string xml)
        {

            if (xml == null)
            {
                throw new ArgumentNullException("xml");
            }

            StringBuilder buffer = new StringBuilder(xml.Length);

            foreach (char c in xml)
            {

                if (c != 0x1B)
                {
                    buffer.Append(c);
                }
            }

            return buffer.ToString();

        }

        private void listboxBoard_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!if_loaded || ((ListBox)sender).SelectedIndex==-1)
                return;
            this.NavigationService.Navigate(new Uri("/ArticleList.xaml?"+"boardname="+listBoard[listboxBoard.SelectedIndex].name,UriKind.Relative));
        }
    }
}